package day10;

//a.模拟火车站售票窗口，要求：票池共100张票，3个售票窗口同时卖票
//售票过程需显示"窗口X卖出第Y张票",使用同步代码块保证票数正确
//当票卖完时所有线程终止
public class Demo1 {
    public static void main(String[] args) {
        int len = 3;
        for (int i = 0; i < len; i++) {
            new Tickets(i, "窗口" + (i + 1)).start();
        }
    }
}

class Tickets extends Thread {
    int id;
    static int sum = 0;

    public Tickets(int id, String name) {
        super(name);
        this.id = id;
    }

    @Override
    public void run() {
        while (sum < 100) {
            synchronized (Tickets.class) {

                try {
                    String name = Thread.currentThread().getName();
                    sum++;
                    System.out.println(name + "卖出第" + sum + "张票");
                    Thread.sleep(10);
                    Tickets.class.notifyAll();
                    if (sum >= 100) {
                        System.out.println("票卖完了!!!");
                        return;
                    }
                    Tickets.class.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
